package ic.symbols;

import ic.ast.decl.Parameter;
import ic.ast.decl.Type;
import ic.ast.stmt.LocalVariable;
import ic.types.TypeTable;

/**
 * Created with IntelliJ IDEA.
 * User: Matan
 * Date: 12/10/13
 * Time: 11:25 AM
 */
public class VarSymbol extends Symbol {

    private Type type;
    private boolean isParameter;

    public VarSymbol(LocalVariable localVariable) throws Exception {
        super(localVariable.getName(), SymbolType.VARIABLE, localVariable.getLine());
        this.isParameter = false;
        this.type = TypeTable.getType(localVariable.getType());
    }

    public VarSymbol(Parameter parameter) throws Exception {
        super(parameter.getName(), SymbolType.VARIABLE, parameter.getLine());
        this.isParameter = true;
        this.type = TypeTable.getType(parameter.getType());
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append(isParameter() ? "Parameter:  " : "Local variable:  ");
        str.append(getID());
        str.append(" : ");
        str.append(getType());
        return str.toString();
    }

    @Override
    public boolean equals(Object other) {
        return this == other;
    }

    public boolean isParameter() {
        return this.isParameter;
    }

    public Type getType() {
        return this.type;
    }

}

